package com.bookstore.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.bookstore.model.ReceiverAddress;
import com.bookstore.utils.DBConnection;

public class ReceiverAddressDAOImpl implements ReceiverAddressDAO {

	private static final String FIND_BY_ID = "select * from d_receive_address where id = ?";
	private static final String SAVE = "insert into d_receive_address (user_id, receive_name, full_address, postal_code, mobile, phone) values (?,?,?,?,?,?)";
	private static final String FIND_BY_USERID = "select * from d_receive_address where user_id = ?";

	@Override
	public ReceiverAddress findById(int id) throws Exception {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		ReceiverAddress address = null;
		try {
			connection = DBConnection.getConnection();
			statement = connection.prepareStatement(FIND_BY_ID);
			statement.setInt(1, id);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				address = new ReceiverAddress();
				address.setId(resultSet.getInt("id"));
				address.setFullAddress(resultSet.getString("full_address"));
				address.setMobile(resultSet.getString("mobile"));
				address.setPhone(resultSet.getString("phone"));
				address.setPostalCode(resultSet.getString("postal_code"));
				address.setReceiveName(resultSet.getString("receive_name"));
				address.setUserId(resultSet.getInt("user_id"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(resultSet, statement, connection);
		}
		return address;
	}

	@Override
	public void save(ReceiverAddress receiverAddress) throws Exception {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			connection = DBConnection.getConnection();
			statement = connection.prepareStatement(SAVE, Statement.RETURN_GENERATED_KEYS);
			statement.setInt(1, receiverAddress.getUserId());
			statement.setString(2, receiverAddress.getReceiveName());
			statement.setString(3, receiverAddress.getFullAddress());
			statement.setString(4, receiverAddress.getPostalCode());
			statement.setString(5, receiverAddress.getMobile());
			statement.setString(6, receiverAddress.getPhone());
			statement.executeUpdate();
			resultSet = statement.getGeneratedKeys();
			resultSet.next();
			int id = resultSet.getInt(1);
			receiverAddress.setId(id);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(resultSet, statement, connection);
		}
	}

	@Override
	public List<ReceiverAddress> findByUserId(int userId) throws Exception {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		List<ReceiverAddress> list = new ArrayList<>();
		ReceiverAddress address = null;
		try {
			connection = DBConnection.getConnection();
			statement = connection.prepareStatement(FIND_BY_USERID);
			statement.setInt(1, userId);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				address = new ReceiverAddress();
				address.setId(resultSet.getInt("id"));
				address.setFullAddress(resultSet.getString("full_address"));
				address.setMobile(resultSet.getString("mobile"));
				address.setPhone(resultSet.getString("phone"));
				address.setPostalCode(resultSet.getString("postal_code"));
				address.setReceiveName(resultSet.getString("receive_name"));
				address.setUserId(resultSet.getInt("user_id"));
				list.add(address);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(resultSet, statement, connection);
		}
		return list;
	}

}
